/**
 *
 */
package name.yzhu.index.classify;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import name.yzhu.common.EnvVariable;

/**
 * @author yhzhu
 *
 */
public class TrainingDataManager {
	static private final Log LOG =  LogFactory.getLog(TrainingDataManager.class.getName());
	private String[] traningFileClassifications;
	private File traningTextDir;
	private static String defaultPath = null;
	private Charset charset = null;

	public TrainingDataManager(){
		traningTextDir = new File(getDefaultDataPath());
		if (!traningTextDir.isDirectory())	{
			throw new IllegalArgumentException("load train data failed [" +defaultPath + "]");
		}
		this.traningFileClassifications = traningTextDir.list(new FilenameFilter(){
			@Override
			public boolean accept(File f, String s) {
				return f.isDirectory();
			}});
	}

	private String getDefaultDataPath(){
		String defPath = System.getProperty(
				name.yzhu.index.Constant.CLASSIFY_TRAIN_DATA_PATH);
		if (null != defPath)
			return defPath;
		URL url = getClass().getClassLoader().getResource(
				name.yzhu.index.Constant.CLASSIFY_TRAIN_DATA_PATH_NAME);
		if (null != url)
			return url.getFile();
		else
			return EnvVariable.getWorkingDirectory()+File.separator
			  +name.yzhu.index.Constant.CLASSIFY_TRAIN_DATA_PATH_NAME;
	}
	/**
	* 返回训练文本类别，这个类别就是目录名
	* @return 训练文本类别
	*/
	public String[] getTraningClassifications()	{
		return this.traningFileClassifications;
	}
	/**
	* 根据训练文本类别返回这个类别下的所有训练文本路径（full path）
	* @param classification 给定的分类
	* @return 给定分类下所有文件的路径（full path）
	*/
	public String[] getFilesPath(String classification)	{
		File classDir = new File(traningTextDir.getPath() +File.separator +classification);
		String[] ret = classDir.list();
		for (int i = 0; i < ret.length; i++)		{
			ret[i] = traningTextDir.getPath() +File.separator +classification +File.separator +ret[i];
		}
		return ret;
	}

	/**
	* 返回给定路径的文本文件内容
	* @param filePath 给定的文本文件路径
	* @return 文本内容
	* @throws java.io.IOException
	*/
	public String getText(String filePath) throws IOException{
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(filePath), charset));
			String aline = null;
			StringBuilder sb = new StringBuilder();
			while ((aline = reader.readLine()) != null){
				sb.append(aline + " ");
			}
			reader.close();
			return sb.toString();
		}
		finally {
			if (null != reader){
				reader.close();
				reader = null;
			}
		}
	}

	/**
	* 返回训练文本集中所有的文本数目
	* @return 训练文本集中所有的文本数目
	*/
	public int getTrainingFileCount()	{
		int ret = 0;
		for (String t: traningFileClassifications)		{
			ret +=getTrainingFileCountOfClassification(t);
		}
		return ret;
	}

	/**
	* 返回训练文本集中在给定分类下的训练文本数目
	* @param classification 给定的分类
	* @return 训练文本集中在给定分类下的训练文本数目
	*/
	public int getTrainingFileCountOfClassification(String classification)	{
//		LOG.debug("classification = " + classification);
		File classDir = new File(traningTextDir.getPath() +File.separator +classification);
		return classDir.list().length;
	}

	/**
	* 返回给定分类中包含关键字／词的训练文本的数目
	* @param classification 给定的分类
	* @param key 给定的关键字／词
	* @return 给定分类中包含关键字／词的训练文本的数目
	*/
	public int getCountContainKeyOfClassification(String classification,String key)	{
		int ret = 0;
		try	{
			String[] filePath = getFilesPath(classification);
			for (int j = 0; j < filePath.length; j++) {
				String text = getText(filePath[j]);
				if (text.contains(key))	{
					ret++;
				}
			}
		}
		catch (IOException e){
			LOG.error(e.getMessage(), e);
		}
		return ret;
	}

	public void setCharset(Charset charset) {
		this.charset = charset;
	}
}
